package com.jiepos.mpos.common.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.stereotype.Repository;

import com.jiepos.mpos.common.entity.App_Custom_InfEntity;
import com.jiepos.mpos.core.dao.BaseDao;

import oracle.jdbc.driver.OracleTypes;

@Repository
public class TransRouteInfoDao extends BaseDao<App_Custom_InfEntity> {
	
	@SuppressWarnings({"unchecked","rawtypes"})
	public Map<String,String> getTransRouteInfo(final String login_id,final String act_out_no,final String custom_no,final String custom_level,final String ins_uuid,final String area_uuid,final String service_uuid,final String service_param_uuid,final String crd_ins,final String crd_nature,final double amt,final String service_ins_uuid,final String crd_bin,final String loc_trace_no){
		Map<String,String> map = (Map)  jdbcTemplate.execute(new CallableStatementCreator() {
			public CallableStatement createCallableStatement(Connection con) throws SQLException {
				String storedProc = "{call package_trans_route_pro.pro_main (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}";// 调用的sql
				CallableStatement cs = con.prepareCall(storedProc);
				cs.setString(1, login_id);// 登录账号
				cs.setString(2, act_out_no);// 支付账号
				cs.setString(3, custom_no);// 客户编号
				cs.setString(4, custom_level);// 会员级别
				cs.setString(5, ins_uuid);// 机构UUID
				cs.setString(6, area_uuid);// 区域UUID
				cs.setString(7, service_uuid);// 业务参数UUID
				cs.setString(8, service_param_uuid);// 业务参数UUID
				cs.setString(9, crd_ins);// 发卡行
				cs.setString(10, crd_nature);// 卡性质
				cs.setDouble(11, amt);// 交易金额
				cs.setString(12, service_ins_uuid);// 交易金额
				cs.setString(13, crd_bin);// 卡bin
				cs.setString(14, loc_trace_no);// 本地流水号
				cs.registerOutParameter(15, OracleTypes.VARCHAR);// 渠道终端名称
				cs.registerOutParameter(16, OracleTypes.VARCHAR);// 渠道方商户号
				cs.registerOutParameter(17, OracleTypes.VARCHAR);// 渠道方终端号
				cs.registerOutParameter(18, OracleTypes.VARCHAR);// 机构uuid
				cs.registerOutParameter(19, OracleTypes.VARCHAR);//业务主键				
				cs.registerOutParameter(20, OracleTypes.VARCHAR);// 业务参数主键
				cs.registerOutParameter(21, OracleTypes.VARCHAR);// 地区代码
				cs.registerOutParameter(22, OracleTypes.VARCHAR);// 收单行号
				cs.registerOutParameter(23, OracleTypes.VARCHAR);// 主密钥索引
				cs.registerOutParameter(24, OracleTypes.VARCHAR);// 主密钥	
				cs.registerOutParameter(25, OracleTypes.VARCHAR);// MAC密钥
				cs.registerOutParameter(26, OracleTypes.VARCHAR);// PIN密钥
				cs.registerOutParameter(27, OracleTypes.VARCHAR);// 磁道加密密钥
				cs.registerOutParameter(28, OracleTypes.NUMBER);// 借记卡-最小手续费
				cs.registerOutParameter(29, OracleTypes.NUMBER);//借记卡-扣率				
				cs.registerOutParameter(30, OracleTypes.NUMBER);// 借记卡-最大手续费
				cs.registerOutParameter(31, OracleTypes.NUMBER);// 贷记卡最小手续费
				cs.registerOutParameter(32, OracleTypes.NUMBER);// 贷记卡扣率
				cs.registerOutParameter(33, OracleTypes.NUMBER);// 贷记卡最大手续费
				cs.registerOutParameter(34, OracleTypes.VARCHAR);// 签到标志	
				cs.registerOutParameter(35, OracleTypes.VARCHAR);// 当前批次号
				cs.registerOutParameter(36, OracleTypes.VARCHAR);// 当前流水号
				cs.registerOutParameter(37, OracleTypes.VARCHAR);// 上一批结日期
				cs.registerOutParameter(38, OracleTypes.VARCHAR);// 当前批结日期
				cs.registerOutParameter(39, OracleTypes.VARCHAR);// 通道编号
				cs.registerOutParameter(40, OracleTypes.NUMBER);// 商户手续费
				cs.registerOutParameter(41, OracleTypes.NUMBER);// 成本手续费
				cs.registerOutParameter(42, OracleTypes.INTEGER);// 查询标志位
				cs.registerOutParameter(43, OracleTypes.VARCHAR);// 商户号
				cs.registerOutParameter(44, OracleTypes.VARCHAR);// 终端号
				cs.registerOutParameter(45, OracleTypes.VARCHAR);// 路由机构
				return cs;
			}
		}, new CallableStatementCallback() {
			public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
				cs.execute();
				Map<String,String>	map = new HashMap<String,String>();
				map.put("term_nm", cs.getString(15));
				map.put("trf_mchnt_cd", cs.getString(16));
				map.put("trf_term_id", cs.getString(17));
				map.put("trf_ins_uuid", cs.getString(18));
				map.put("trf_service_uuid", cs.getString(19));
				map.put("trf_service_param_uuid", cs.getString(20));
				map.put("area_cd", cs.getString(21));
				map.put("acq_ins_cd", cs.getString(22));
				map.put("master_key_idx", cs.getString(23));
				map.put("master_key", cs.getString(24));
				map.put("mac_key", cs.getString(25));
				map.put("pin_key", cs.getString(26));
				map.put("trk_key", cs.getString(27));
				map.put("debit_minfee", String.valueOf(cs.getDouble(28)));
				map.put("debit_rate", String.valueOf(cs.getDouble(29)));
				map.put("debit_maxfee", String.valueOf(cs.getDouble(30)));
				map.put("credit_minfee", String.valueOf(cs.getDouble(31)));
				map.put("credit_rate", String.valueOf(cs.getDouble(32)));
				map.put("credit_maxfee", String.valueOf(cs.getDouble(33)));
				map.put("sign_st", cs.getString(34));
				map.put("curr_batch_no", cs.getString(35));
				map.put("curr_trace_no", cs.getString(36));
				map.put("prev_cycle_dt", cs.getString(37));
				map.put("curr_cycle_dt", cs.getString(38));
				map.put("channel_No", cs.getString(39));
				map.put("midfee", String.valueOf(cs.getDouble(40)));
				map.put("costfee", String.valueOf(cs.getDouble(41)));
				map.put("resultNum", String.valueOf(cs.getInt(42)));
				map.put("mchnt_cd", cs.getString(43));
				map.put("term_id", cs.getString(44));
				map.put("route_ins_uuid", cs.getString(45));
				log.debug("路由查询对应的信息:[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]",login_id,act_out_no,custom_no,custom_level,ins_uuid,area_uuid,service_uuid,service_param_uuid,crd_ins,crd_nature,amt,service_ins_uuid,crd_bin,loc_trace_no,map);
				return map;// 获取输出参数的值
			}
		});
		return map;
	}

}
